package cn.edu.sdjzu.xg21.service;

import cn.edu.sdjzu.xg21.dao.TeacherDao;
import cn.edu.sdjzu.xg21.domain.Teacher;
import cn.edu.sdjzu.xg21.exception.BysjException;
import cn.edu.sdjzu.xg21.util.Condition;
import cn.edu.sdjzu.xg21.util.JdbcHelper;
import cn.edu.sdjzu.xg21.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class TeacherService implements TeacherServiceInterface{
    // 构造器私有化
    private TeacherService(){}
    // Teacher实体
    private Teacher teacher = null;
    // Teacher类型集合
    private Collection<Teacher> teachers = null;
    // TeacherDao实例
    private static final TeacherDao teacherDao = TeacherDao.getInstance();
    // TeacherService实例
    private static final TeacherService teacherService = new TeacherService();
    public static TeacherService getInstance() throws SQLException {
        return teacherService;
    }

    @Override
    public Teacher find(int id) throws SQLException, BysjException {
        return TeacherDao.getInstance().find(id);
    }

    @Override
    // 一个参数的findAll
    public Collection<Teacher> findAll(List<Condition> conditions_json) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        teachers = TeacherDao.getInstance().findAll(connection,conditions_json);
        JdbcHelper.close(connection);
        return teachers;
    }
    // 实现接口的findAll（两个参数）
    @Override
    public Collection<Teacher> findAll(List<Condition> conditionList, Pagination pagination) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        Collection<Teacher> teachers = teacherDao.findAll(connection,conditionList,pagination);
        JdbcHelper.close(connection);
        return teachers;
    }

    // 添加一条教师信息
//    @Override
//    public String add(Teacher teacher) throws SQLException,BysjException{
//        Connection connection = JdbcHelper.getConn();
//        connection.setAutoCommit(false);
//        String addMessage = null;
//        try{
//            // 考虑到后期可能会有User，故保留61和63行
//            int teacherId = TeacherDao.getInstance().add(teacher,connection);
//            // 为教师的id属性赋值
//            teacher.setId(teacherId); // 这行之后Teacher的字段值就完整了
//            addMessage = "添加成功";
//            connection.commit();
//        }catch(SQLException e){
//            System.out.println(e.getMessage() + "error:" + e.getErrorCode());
//            e.printStackTrace();
//            connection.rollback();
//        }catch(BysjException e){
//            System.out.println(e.getMessage());
//            e.printStackTrace();
//            connection.rollback();
//        } finally {
//            connection.setAutoCommit(true);
//            JdbcHelper.close(null,connection);
//        }
//        return addMessage;
//    }

    @Override
    public String delete(int id) throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        String deleteMessage = null;
        try{
            boolean deleteOrNot = TeacherDao.getInstance().delete(id,connection);
            if(deleteOrNot == true){
                deleteMessage = "删除成功";
            }else {
                deleteMessage = "删除失败，该教师不存在";
            }
            connection.commit();
        }catch(SQLException e){
            System.out.println(e.getMessage() + "error:" + e.getErrorCode());
            e.printStackTrace();
            connection.rollback();
        }catch(BysjException e){
            System.out.println(e.getMessage());
            e.printStackTrace();
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(null,connection);
        }
        return deleteMessage;
    }

//    @Override
//    public String update(Teacher teacher) throws SQLException,BysjException{
//        Connection connection = JdbcHelper.getConn();
//        connection.setAutoCommit(false);
//        String updateMessage = null;
//        try{
//            boolean updateOrNot = TeacherDao.getInstance().update(teacher,connection);
//            if(updateOrNot == true){
//                updateMessage = "更新成功";
//            }else {
//                updateMessage = "更新失败，该教师不存在";
//            }
//            connection.commit();
//        }catch(SQLException e){
//            System.out.println(e.getMessage() + "error:" + e.getErrorCode());
//            e.printStackTrace();
//            connection.rollback();
//        }catch(BysjException e){
//            System.out.println(e.getMessage());
//            e.printStackTrace();
//            connection.rollback();
//        } finally {
//            connection.setAutoCommit(true);
//            JdbcHelper.close(null,connection);
//        }
//        return updateMessage;
//    }

}

